package com.situ.tos2024.controller;

import com.situ.tos2024.model.User;
import com.situ.tos2024.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
 * 功能：用户管理
 *
 * @author 千堆雪
 * @version 1.0.0
 * @since 2023/12/26
 * <p>
 * created by 千堆雪 on 2023/12/26, last modified by 千堆雪 on 2023/12/26
 */
@Controller
@RequestMapping("/user")
public class UserController {
    private final UserService userService;
    
    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 处理/list请求，列出用户信息列表
     */
    @GetMapping("/list")
    public String list(Map<String, Object> map) {
        return "user/list";
    }

    /**
     * 处理/student/add请求
     */
    @GetMapping("/add")
    public String add(Map<String, Object> map) {
        map.put("showSubmit", false);//是否显示提交按钮
        return "user/add";
    }

    /**
     * 进入修改页面
     */
    @GetMapping("/edit")
    public String edit(Integer id, Map<String, Object> map) {
        User student = userService.findById(id);
        if (student == null) {
            map.put("error", "要修改的用户不存在");
        } else {
            map.put("stu", student);
        }
        return "user/edit";
    }
}
